package com.abo.yaas.i18n;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;

/**
 * This servlet reads the xml file of all translations and loads them into a
 * map.
 */
@SuppressWarnings("serial")
public class InternationalizationServlet extends HttpServlet {

    private static Map<String, Map<String, String>> translations = new HashMap<String, Map<String, String>>();

    /**
     * Adds a translated message to the translations map
     * 
     * @param language
     *            Language of the translation
     * @param identifier
     *            Key string for the translation message
     * @param message
     *            The translated message
     * @param force
     *            If a translation already exists in-memory, should the new
     *            translation override the existing translation
     */
    private static void addMessage(String language, String identifier, String message, boolean force) {
        Map<String, String> messages = null;
        if (!translations.containsKey(language)) {
            messages = new HashMap<String, String>();
            translations.put(language, messages);
        } else {
            messages = translations.get(language);
        }
        if (!messages.containsKey(identifier) || force) {
            messages.put(identifier, message);
        }
    }

    /**
     * Clear all translations from the memory
     */
    public static void clear() {
        translations.clear();
    }

    /**
     * Get the translated message for a specific language
     * 
     * @param language
     *            Language for which we want the translation
     * @param identifier
     *            Key for the translation message
     * @param params
     *            Parameters for the translation message
     * @return Translated message string
     */
    public static String getMessage(String language, String identifier,
            Object... params) {
        if (!translations.containsKey(language)) {
            return "";
        }

        // Get the raw translation message
        final Map<String, String> messages = translations.get(language);
        String msg = messages.containsKey(identifier) ? messages
                .get(identifier) : "";

        // Check if any parameters are defined
        if (params != null) {
            // Replace the placeholders in the raw message with the given
            // parameters
            for (int i = 0; i < params.length; i++) {
                final String value = String.valueOf(params[i]);
                msg = msg.replace("{" + i + "}", value);
            }
        }

        return msg;
    }

    /**
     * Load translations from the source into memory. Any duplicated
     * translations will be discarded.
     * 
     * @param source
     *            The source for translation message
     */
    public static void loadTranslations(TranslationSource source) {
        loadTranslations(source, false);
    }

    /**
     * Load a translation file into memory.
     * 
     * @param source
     *            The source for translation message
     * @param force
     *            If a translation already exists in-memory, should the new
     *            translation override the existing translation
     */
    public static void loadTranslations(TranslationSource source, boolean force) {
        if (source == null) {
            throw new IllegalArgumentException("Source did not exist");
        }

        while (source.hasNext()) {
            final TranslationMessage message = source.getNext();
            addMessage(message.getLanguage(), message.getTuid(),
                    message.getValue(), force);
        }
    }
}
